/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <cstdio>
#include <fcntl.h>
#include <unistd.h>

#include <gtest/gtest.h>

#include "securec.h"

#include "app_rsa.h"
#include "app_sha256.h"
#include "hota_updater.h"
#include "hota_verify.h"

using namespace std;
using namespace testing::ext;

static const int READ_BUF_LEN = 256;
static char g_readBuf[READ_BUF_LEN];
static const int SIGN_RSA2048_LEN = 256;
static const int SIGN_RSA3072_LEN = 384;

static const int OTA_PKG_SIZE = 16 * 58 + 11;
static const char *PASS_THROUGH_PATH = "/sdcard/update/ota_pkg_pass_through.bin";
static const int INFO_COMP_DATA_LEN = 267;
static const int WRONG_PKG_TYPE = 128;

/* this pkg is packed by same pubkey */
static char g_otaPkg[OTA_PKG_SIZE] = {
    0x11, 0x00, 0x88, 0x00, 0x0b, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x68, 0x69, 0x73, 0x69,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x70, 0x65, 0x6e,
    0x48, 0x61, 0x72, 0x6d, 0x6f, 0x6e, 0x79, 0x20, 0x39, 0x39, 0x2e, 0x39, 0x39, 0x2e, 0x39, 0x39,
    0x2e, 0x39, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x20, 0x00,
    0x32, 0x30, 0x32, 0x31, 0x2e, 0x30, 0x37, 0x2e, 0x30, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x31, 0x30, 0x3a, 0x33, 0x36, 0x3a, 0x33, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x05, 0x00, 0x47, 0x00, 0x6f, 0x74, 0x61, 0x5f, 0x74, 0x61, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x05, 0x00, 0x00, 0x31, 0x2e, 0x30, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc3, 0x22, 0xf9, 0x3f, 0x69,
    0x5d, 0x8f, 0xc6, 0x30, 0xa7, 0xb1, 0x62, 0xf1, 0x41, 0xd2, 0xf4, 0xef, 0x2a, 0x3a, 0xc8, 0xc3,
    0x50, 0x99, 0x56, 0xe8, 0x57, 0x7a, 0xcc, 0x48, 0xa5, 0xac, 0x69, 0x75, 0x70, 0x64, 0x61, 0x74,
    0x65, 0x2f, 0x69, 0x6e, 0x66, 0x6f, 0x2e, 0x62, 0x69, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x24, 0xce, 0x42, 0x75, 0xef,
    0x2e, 0xcf, 0x9c, 0xc7, 0xee, 0x90, 0xe9, 0x57, 0xb3, 0x63, 0x99, 0xd6, 0x32, 0xa4, 0x03, 0x54,
    0xe4, 0x38, 0xe4, 0xf2, 0x41, 0x28, 0x3d, 0x40, 0xa5, 0x83, 0xb8, 0xbd, 0xaf, 0x7b, 0xa9, 0xdd,
    0x7f, 0xa9, 0xb3, 0x2e, 0x78, 0x66, 0x85, 0xe6, 0x9d, 0xda, 0xb2, 0x1d, 0x1b, 0xaa, 0xd4, 0xa6,
    0x07, 0xb8, 0xb4, 0xe3, 0xff, 0x36, 0x3a, 0x02, 0x80, 0xc6, 0xae, 0x56, 0x22, 0x40, 0x24, 0xf1,
    0x22, 0x7a, 0x04, 0xf8, 0x2a, 0x3a, 0x88, 0xe6, 0x7e, 0xde, 0x46, 0x8e, 0x5e, 0x14, 0x9c, 0xce,
    0xa6, 0x92, 0x64, 0x65, 0x70, 0x9e, 0x2f, 0x70, 0x39, 0x46, 0xa8, 0x1e, 0x85, 0x55, 0x5a, 0xea,
    0x81, 0xc1, 0x22, 0x5d, 0xe7, 0xda, 0xbf, 0x28, 0x1e, 0x78, 0xa4, 0x8d, 0x66, 0xa3, 0x12, 0x9a,
    0xb8, 0x0b, 0xb9, 0x4e, 0xfc, 0xa4, 0x66, 0x28, 0xf3, 0x3a, 0xd9, 0x72, 0x94, 0x8f, 0x04, 0x9a,
    0x58, 0xf6, 0xcb, 0xf4, 0xee, 0x55, 0xf0, 0x39, 0x98, 0x8e, 0x08, 0x91, 0x82, 0x9c, 0x63, 0xed,
    0x4c, 0xb2, 0xa1, 0xb2, 0xd2, 0x3f, 0xc7, 0x68, 0xcf, 0x3c, 0x75, 0x63, 0xb3, 0x9b, 0x9e, 0x99,
    0x71, 0xcc, 0x47, 0x5a, 0x2c, 0xb8, 0xe4, 0x77, 0x17, 0x10, 0x66, 0x1e, 0x48, 0x17, 0xc5, 0x24,
    0x09, 0x1a, 0x8f, 0xbd, 0x77, 0x2f, 0x4a, 0xba, 0x7a, 0x4e, 0x1c, 0x68, 0xb6, 0xdc, 0x76, 0x68,
    0x73, 0x38, 0xfe, 0x8e, 0xf8, 0xfe, 0x67, 0xe7, 0x3d, 0xe4, 0x29, 0x9a, 0x91, 0xc3, 0x3a, 0x73,
    0x35, 0x5c, 0x2f, 0xb9, 0x3d, 0x5e, 0x03, 0x44, 0x95, 0x3f, 0x7d, 0x3e, 0x9f, 0x29, 0x85, 0xc6,
    0x8c, 0x4a, 0x25, 0x5d, 0xc6, 0xc4, 0xb7, 0x08, 0xdb, 0x87, 0x22, 0x61, 0x30, 0x2d, 0xba, 0xe5,
    0xc1, 0x2f, 0x62, 0xc3, 0x7c, 0x70, 0xab, 0x41, 0xc9, 0x93, 0xee, 0x6e, 0xcc, 0x74, 0x85, 0x37,
    0x61, 0xdc, 0x36, 0x98, 0x0e, 0xad, 0x67, 0x89, 0xdc, 0x94, 0xcb, 0xd1, 0xab, 0xc1, 0x71, 0x1c,
    0x76, 0x18, 0x20, 0x73, 0xe8, 0x61, 0x55, 0xcb, 0x6a, 0xae, 0x2c, 0x2d, 0x10, 0x0c, 0x40, 0xf0,
    0x17, 0xd8, 0xd1, 0x8d, 0x2e, 0x9b, 0xef, 0xca, 0x97, 0xc7, 0x21, 0x7f, 0x58, 0x5a, 0xdc, 0x6c,
    0x1d, 0x9b, 0x57, 0x6a, 0x48, 0xd2, 0xc0, 0x6a, 0xa8, 0x4d, 0x95, 0x5b, 0x2e, 0x11, 0x6d, 0x7a,
    0x30, 0x96, 0xce, 0x7e, 0x33, 0x55, 0x4f, 0x8e, 0x01, 0x67, 0xb6, 0x8f, 0x1c, 0x44, 0x46, 0x26,
    0xa6, 0xab, 0x57, 0x6f, 0xe6, 0x22, 0x2e, 0xe0, 0xac, 0x3d, 0x78, 0x5c, 0x6a, 0xf2, 0xe2, 0xc9,
    0xc6, 0x89, 0xcb, 0x0d, 0xc0, 0x93, 0x5c, 0xc1, 0x5b, 0x1e, 0x75, 0x64, 0xcf, 0xbd, 0x35, 0x26,
    0x90, 0x98, 0xd5, 0x61, 0x47, 0xef, 0x18, 0x10, 0x4f, 0x33, 0x75, 0x70, 0x61, 0x63, 0x6b, 0x61,
    0x67, 0x65, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x3a, 0x6f, 0x74, 0x61, 0x41, 0x31, 0x53, 0x32, 0x44,
    0x33, 0x46, 0x34, 0x47, 0x35, 0x48, 0x36, 0x4a, 0x37, 0x4b, 0x38
};

/* this pkg is packed by different pubkey */
static char g_otaPkgWrongKey[OTA_PKG_SIZE] = {
    0x11, 0x00, 0x88, 0x00, 0x0b, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x68, 0x69, 0x73, 0x69,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x70, 0x65, 0x6e,
    0x48, 0x61, 0x72, 0x6d, 0x6f, 0x6e, 0x79, 0x20, 0x39, 0x39, 0x2e, 0x39, 0x39, 0x2e, 0x39, 0x39,
    0x2e, 0x39, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x20, 0x00,
    0x32, 0x30, 0x32, 0x31, 0x2e, 0x30, 0x37, 0x2e, 0x30, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x31, 0x30, 0x3a, 0x33, 0x36, 0x3a, 0x33, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x05, 0x00, 0x47, 0x00, 0x6f, 0x74, 0x61, 0x5f, 0x74, 0x61, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x05, 0x00, 0x00, 0x31, 0x2e, 0x30, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc3, 0x22, 0xf9, 0x3f, 0x69,
    0x5d, 0x8f, 0xc6, 0x30, 0xa7, 0xb1, 0x62, 0xf1, 0x41, 0xd2, 0xf4, 0xef, 0x2a, 0x3a, 0xc8, 0xc3,
    0x50, 0x99, 0x56, 0xe8, 0x57, 0x7a, 0xcc, 0x48, 0xa5, 0xac, 0x69, 0x75, 0x70, 0x64, 0x61, 0x74,
    0x65, 0x2f, 0x69, 0x6e, 0x66, 0x6f, 0x2e, 0x62, 0x69, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x24, 0xce, 0x42, 0x75, 0xef,
    0x2e, 0xcf, 0x9c, 0xc7, 0xee, 0x90, 0xe9, 0x57, 0xb3, 0x63, 0x99, 0xd6, 0x32, 0xa4, 0x03, 0x54,
    0xe4, 0x38, 0xe4, 0xf2, 0x41, 0x28, 0x3d, 0x40, 0xa5, 0x83, 0xb8, 0xbd, 0xaf, 0x7b, 0xa9, 0xdd,
    0x7f, 0xa9, 0xb3, 0x2e, 0x78, 0x66, 0x85, 0xe6, 0x9d, 0xda, 0xb2, 0x1d, 0x1b, 0xaa, 0xd4, 0xa6,
    0x07, 0xb8, 0xb4, 0xe3, 0xff, 0x36, 0x3a, 0x02, 0x80, 0xc6, 0xae, 0x56, 0x22, 0x40, 0x24, 0xf1,
    0x22, 0x7a, 0x04, 0xf8, 0x2a, 0x3a, 0x88, 0xe6, 0x7e, 0xde, 0x46, 0x8e, 0x5e, 0x14, 0x9c, 0xce,
    0xa6, 0x92, 0x64, 0x65, 0x70, 0x9e, 0x2f, 0x70, 0x39, 0x46, 0xa8, 0x1e, 0x85, 0x55, 0x5a, 0xea,
    0x81, 0xc1, 0x22, 0x5d, 0xe7, 0xda, 0xbf, 0x28, 0x1e, 0x78, 0xa4, 0x8d, 0x66, 0xa3, 0x12, 0x9b,
    0xb8, 0x0b, 0xb9, 0x4e, 0xfc, 0xa4, 0x66, 0x28, 0xf3, 0x3a, 0xd9, 0x72, 0x94, 0x8f, 0x04, 0x9b,
    0x58, 0xf6, 0xcb, 0xf4, 0xee, 0x55, 0xf0, 0x39, 0x98, 0x8e, 0x08, 0x91, 0x82, 0x9c, 0x63, 0xee,
    0x4c, 0xb2, 0xa1, 0xb2, 0xd2, 0x3f, 0xc7, 0x68, 0xcf, 0x3c, 0x75, 0x63, 0xb3, 0x9b, 0x9e, 0x9a,
    0x71, 0xcc, 0x47, 0x5a, 0x2c, 0xb8, 0xe4, 0x77, 0x17, 0x10, 0x66, 0x1e, 0x48, 0x17, 0xc5, 0x25,
    0x09, 0x1a, 0x8f, 0xbd, 0x77, 0x2f, 0x4a, 0xba, 0x7a, 0x4e, 0x1c, 0x68, 0xb6, 0xdc, 0x76, 0x69,
    0x73, 0x38, 0xfe, 0x8e, 0xf8, 0xfe, 0x67, 0xe7, 0x3d, 0xe4, 0x29, 0x9a, 0x91, 0xc3, 0x3a, 0x74,
    0x35, 0x5c, 0x2f, 0xb9, 0x3d, 0x5e, 0x03, 0x44, 0x95, 0x3f, 0x7d, 0x3e, 0x9f, 0x29, 0x85, 0xc7,
    0x8c, 0x4a, 0x25, 0x5d, 0xc6, 0xc4, 0xb7, 0x08, 0xdb, 0x87, 0x22, 0x61, 0x30, 0x2d, 0xba, 0xe6,
    0xc1, 0x2f, 0x62, 0xc3, 0x7c, 0x70, 0xab, 0x41, 0xc9, 0x93, 0xee, 0x6e, 0xcc, 0x74, 0x85, 0x38,
    0x61, 0xdc, 0x36, 0x98, 0x0e, 0xad, 0x67, 0x89, 0xdc, 0x94, 0xcb, 0xd1, 0xab, 0xc1, 0x71, 0x1d,
    0x76, 0x18, 0x20, 0x73, 0xe8, 0x61, 0x55, 0xcb, 0x6a, 0xae, 0x2c, 0x2d, 0x10, 0x0c, 0x40, 0xf1,
    0x17, 0xd8, 0xd1, 0x8d, 0x2e, 0x9b, 0xef, 0xca, 0x97, 0xc7, 0x21, 0x7f, 0x58, 0x5a, 0xdc, 0x6d,
    0x1d, 0x9b, 0x57, 0x6a, 0x48, 0xd2, 0xc0, 0x6a, 0xa8, 0x4d, 0x95, 0x5b, 0x2e, 0x11, 0x6d, 0x7b,
    0x30, 0x96, 0xce, 0x7e, 0x33, 0x55, 0x4f, 0x8e, 0x01, 0x67, 0xb6, 0x8f, 0x1c, 0x44, 0x46, 0x27,
    0xa6, 0xab, 0x57, 0x6f, 0xe6, 0x22, 0x2e, 0xe0, 0xac, 0x3d, 0x78, 0x5c, 0x6a, 0xf2, 0xe2, 0xca,
    0xc6, 0x89, 0xcb, 0x0d, 0xc0, 0x93, 0x5c, 0xc1, 0x5b, 0x1e, 0x75, 0x64, 0xcf, 0xbd, 0x35, 0x26,
    0x90, 0x98, 0xd5, 0x61, 0x47, 0xef, 0x18, 0x10, 0x4f, 0x33, 0x75, 0x70, 0x61, 0x63, 0x6b, 0x61,
    0x67, 0x65, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x3a, 0x6f, 0x74, 0x61, 0x41, 0x31, 0x53, 0x32, 0x44,
    0x33, 0x46, 0x34, 0x47, 0x35, 0x48, 0x36, 0x4a, 0x37, 0x4b, 0x38
};


static const char *HotaGetOtaPkg(bool isValid)
{
    return isValid ? g_otaPkg : g_otaPkgWrongKey;
}

static int HotaInterfaceTest()
{
    (void)HotaSetPackageType(USE_DEFAULT_PKG);
    (void)HotaGetUpdateIndex(NULL);
    (void)HotaInit(NULL, NULL);
    (void)HotaWrite(NULL, 0, 0);
    (void)HotaRead(0, 0, NULL);
    (void)HotaCancel();
    (void)HotaRebootAndCleanUserData();
    (void)HotaRebootAndCleanCache();
    return 0;
}

static int HotaWriteTest(const char *otaPkg, int len, int pkgOffset)
{
    int leftLen = len;
    int offset = pkgOffset;
    while (leftLen > 0) {
        int tmpLen = ((leftLen >= READ_BUF_LEN) ? READ_BUF_LEN : leftLen);
        (void)memset_s(g_readBuf, READ_BUF_LEN, 0, READ_BUF_LEN);
        if (memcpy_s(g_readBuf, READ_BUF_LEN, otaPkg + offset, tmpLen) != EOK) {
            printf("memcpy fail!\r\n");
            (void)HotaCancel();
            return -1;
        }
        if (HotaWrite(reinterpret_cast<unsigned char *>(g_readBuf), offset, tmpLen) != 0) {
            printf("ota write fail!\r\n");
            (void)HotaCancel();
            return -1;
        }
        offset += READ_BUF_LEN;
        leftLen -= tmpLen;
    }
    return 0;
}

/* isValid: true: use valid pkg, false: use invalid pkg */
static int HotaByDefaultPkg(bool isValid)
{
    if (HotaInit(NULL, NULL) < 0) {
        printf("ota update init fail!\r\n");
        return -1;
    }
    HotaSetPackageType(USE_DEFAULT_PKG);
    int offset = 0;
    int pkgLen = OTA_PKG_SIZE;
    int leftLen = pkgLen;
    const char *otaPkg = HotaGetOtaPkg(isValid);
    if (HotaWriteTest(otaPkg, leftLen, offset) == -1) {
        return -1;
    }
    return 0;
}

static int HotaByDefaultPkgWithWrongHash()
{
    g_otaPkg[INFO_COMP_DATA_LEN - 1] += 1;
    int ret = HotaByDefaultPkg(true);
    g_otaPkg[INFO_COMP_DATA_LEN - 1] -= 1;
    return ret;
}

static int HotaByDefaultPkgWithWrongKey()
{
    return HotaByDefaultPkg(false);
}

static int HotaReadTest(const char *otaPkg, int len, int pkgOffset)
{
    int leftLen = len;
    int offset = pkgOffset;
    while (leftLen > 0) {
        int tmpLen = ((leftLen >= READ_BUF_LEN) ? READ_BUF_LEN : leftLen);
        (void)memset_s(g_readBuf, READ_BUF_LEN, 0, READ_BUF_LEN);
        if (HotaRead(offset, READ_BUF_LEN, reinterpret_cast<unsigned char *>(g_readBuf)) != 0) {
            printf("ota write fail!\r\n");
            (void)HotaCancel();
            return -1;
        }
        if (memcmp(g_readBuf, otaPkg + offset, tmpLen) != 0) {
            printf("file content cmp fail!\r\n");
            (void)HotaCancel();
            return -1;
        }
        offset += READ_BUF_LEN;
        leftLen -= tmpLen;
    }

    return 0;
}

static int HotaByNotDefaultPkg()
{
    if (HotaInit(NULL, NULL) < 0) {
        printf("ota update init fail!\r\n");
        return -1;
    }
    HotaSetPackageType(NOT_USE_DEFAULT_PKG);
    int offset = 0;
    int pkgLen = OTA_PKG_SIZE;
    int leftLen = pkgLen;
    const char *otaPkg = HotaGetOtaPkg(true);
    if (HotaWriteTest(otaPkg, leftLen, offset) == -1) {
        return -1;
    }

    int fd = open(PASS_THROUGH_PATH, O_RDWR, S_IRUSR | S_IWUSR);
    if (fd < 0) {
        printf("file open failed, fd = %d\r\n", fd);
        (void)HotaCancel();
        return -1;
    }
    int newLen = lseek(fd, 0, SEEK_END);
    if (pkgLen != newLen) {
        printf("pkg len wrong! old = %d, new = %d\r\n", pkgLen, newLen);
        (void)HotaCancel();
        close(fd);
        return -1;
    }
    close(fd);
    leftLen = pkgLen;
    offset = 0;
    if (HotaReadTest(otaPkg, leftLen, offset) == -1) {
        return -1;
    }
    return 0;
}

static int HotaCalcImageHash(uint8 *dataAddr, uint32 dataLen, uint8 *hash, uint32 hashLen)
{
    AppSha256Context sha256;
    uint32 count;

    if ((dataAddr == nullptr) || (hash == nullptr) || (dataLen == 0) || (hashLen < HASH_LENGTH)) {
        return -1;
    }

    uint8 *dataBuff = dataAddr;
    if (memset_s(hash, hashLen, 0, hashLen) != EOK) {
        return -1;
    }
    AppSha256Init(&sha256);

    while (dataLen > 0) {
        count = (dataLen > BUFFR_LENGTH) ? BUFFR_LENGTH : dataLen;
        AppSha256Update(&sha256, dataBuff, count);
        dataLen -= count;
        dataBuff += count;
    }

    AppSha256Finish(&sha256, hash);
    return 0;
}

class OtaTest : public testing::Test {
public:
    static void SetUpTestCase(void);
    static void TearDownTestCase(void);
    void SetUp();
    void TearDown();
};

void OtaTest::SetUpTestCase(void)
{
}

void OtaTest::TearDownTestCase(void)
{
}

void OtaTest::SetUp(void)
{
}

void OtaTest::TearDown(void)
{
    (void)HotaCancel();
}


HWTEST_F(OtaTest, interface_call_001, TestSize.Level4)
{
    EXPECT_EQ(0, HotaInterfaceTest());
}


HWTEST_F(OtaTest, default_pkg_001, TestSize.Level0)
{
    EXPECT_EQ(0, HotaByDefaultPkg(true));
}


HWTEST_F(OtaTest, not_default_pkg_001, TestSize.Level2)
{
    EXPECT_EQ(0, HotaByNotDefaultPkg());
}


HWTEST_F(OtaTest, default_pkg_wrong_hash_001, TestSize.Level3)
{
    EXPECT_EQ(-1, HotaByDefaultPkgWithWrongHash());
}


HWTEST_F(OtaTest, default_pkg_wrong_key_001, TestSize.Level3)
{
    EXPECT_EQ(-1, HotaByDefaultPkgWithWrongKey());
}


HWTEST_F(OtaTest, set_wrong_type_001, TestSize.Level3)
{
    EXPECT_EQ(-1, HotaSetPackageType(WRONG_PKG_TYPE));
}


HWTEST_F(OtaTest, hota_sign_verify_001, TestSize.Level3)
{
    EXPECT_EQ(0, HotaSignVerify(reinterpret_cast<uint8 *>(g_otaPkg), INFO_COMP_DATA_LEN,
        reinterpret_cast<uint8 *>(g_otaPkg + INFO_COMP_DATA_LEN + SIGN_RSA2048_LEN), SIGN_RSA3072_LEN));
}


HWTEST_F(OtaTest, hota_get_hash_001, TestSize.Level3)
{
    unsigned char hashOut[HASH_LENGTH] = {0};
    EXPECT_EQ(0, HotaGetHash(hashOut, HASH_LENGTH));
}


HWTEST_F(OtaTest, hota_get_key_001, TestSize.Level3)
{
    uint32 length = 0;
    uint8 *keyBuf = HotaGetPubKey(&length);
    EXPECT_NE(0, length);
    EXPECT_NE(true, keyBuf == nullptr);
}


HWTEST_F(OtaTest, hota_decode_key_001, TestSize.Level3)
{
    AppRsaContext rsa;
    uint32 length = 0;

    AppRsaInit(&rsa);
    uint8 *keyBuf = HotaGetPubKey(&length);
    EXPECT_NE(0, length);
    EXPECT_NE(true, keyBuf == nullptr);

    int ret = AppRsaDecodePublicKey(&rsa, keyBuf, length);
    AppRsaFree(&rsa);
    EXPECT_EQ(0, ret);
}


HWTEST_F(OtaTest, hota_verify_data_001, TestSize.Level3)
{
    uint32 length = 0;
    AppRsaContext rsa;

    AppRsaInit(&rsa);
    uint8 *keyBuf = HotaGetPubKey(&length);
    EXPECT_NE(true, keyBuf == nullptr);
    EXPECT_NE(0, length);

    int ret = AppRsaDecodePublicKey(&rsa, keyBuf, length);
    EXPECT_EQ(0, ret);

    uint8 imageHashLocal[HASH_LENGTH] = {0};
    ret = HotaCalcImageHash(reinterpret_cast<uint8 *>(g_otaPkg), INFO_COMP_DATA_LEN, imageHashLocal, HASH_LENGTH);
    EXPECT_EQ(0, ret);

    ret = AppVerifyData(&rsa, imageHashLocal, HASH_LENGTH,
        reinterpret_cast<uint8 *>(g_otaPkg + INFO_COMP_DATA_LEN + SIGN_RSA2048_LEN), SIGN_RSA3072_LEN);
    AppRsaFree(&rsa);
    EXPECT_EQ(0, ret);
}
